package com.WISSC.controller;


import com.WISSC.bean.*;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.commen.BaseController;
import com.WISSC.service.ConfigureService;
import com.WISSC.service.OccupationImageService;
import com.WISSC.service.OccupationService;
import com.WISSC.service.WebPageService;
import com.WISSC.util.PageResult;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.file.DownLoadFileUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 职业资格信息表 前端控制器
 * </p>
 *
 * @author 张凯
 * @since 2019-05-06
 */
@Controller
@RequestMapping("/occupation")
@Api(value = "职业资格考核 controller", tags = "职业资格考核")
public class OccupationController extends BaseController {

    @Autowired
    OccupationService occupationService;

    @Autowired
    OccupationImageService occupationImageService;

    @Autowired
    WebPageService webPageService;

    @Autowired
    ConfigureService configureService;

    //跳转数据表格页面
    @RequestMapping(value = "main.html", method = RequestMethod.GET)
    public String occupation() {
        return "main/occupation";
    }

    //跳转添加页面
    @RequestMapping(value = "occupation_add.html", method = RequestMethod.GET)
    public String occupation_add() {
        return "add/occupation_add";
    }

    //跳转编辑页面
    @RequestMapping(value = "occupation_edit.html", method = RequestMethod.GET)
    public String occupation_edit(String id, Model model, String url, String occId) {
//        OccupationImage occupationImage = occupationImageService.getPictureUrl(id);
        model.addAttribute("id", id);
        model.addAttribute("url", url);
        model.addAttribute("occId", occId);
        return "edit/occupation_edit";
    }

    //教员查询登陆页面
    @RequestMapping(value = "occupation_login.html", method = RequestMethod.GET)
    public String occupation_login() {

        return "occupation_login";
    }

    //教员查询首页
    @RequestMapping(value = "occupation_index.html", method = RequestMethod.GET)
    public String occupation_index(Model model, Occupation occupation) {
        model.addAttribute("idNumber", occupation.getIdNumber());
        model.addAttribute("idNo", occupation.getIdNo());
        model.addAttribute("list", webPageService.selectAll());
        model.addAttribute("url", configureService.getById(1).getConfigure());
        model.addAttribute("count1", occupationService.getCount(occupation, 1));
        model.addAttribute("count2", occupationService.getCount(occupation, 2));
        model.addAttribute("count3", occupationService.getCount(occupation, 3));
        model.addAttribute("count4", occupationService.getCount(occupation, 4));
        model.addAttribute("count5", occupationService.getCount(occupation, 5));
        return "occupation_index";
    }

    //跳转教练官资格证书页面
    @RequestMapping(value = "coach.html", method = RequestMethod.GET)
    public String coach(Model model, Occupation occupation, PageResult p) {
        int num = occupationService.getOccupationCount(occupation.getIdNumber(), occupation.getIdNo());
        PageResult pr = new PageResult(p.getCurPage(), num);
        Map<String, Object> map = new HashMap<>();
        map.put("p", pr);
        map.put("qualifications", 1);
        map.put("idNumber", occupation.getIdNumber());
        map.put("idNo", occupation.getIdNo());
        List<Occupation> list = occupationService.getImageUrl(map);
        List<Occupation> list2 = occupationService.getImageUrl1(map);
        List list1 = new ArrayList();
        for (int i = 0; i < list2.size(); i++) {
            list1.add(i + 1);
        }
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        model.addAttribute("idNo", occupation.getIdNo());
        model.addAttribute("idNumber", occupation.getIdNumber());
        model.addAttribute("p", pr);
        return "qualification/coach";
    }

    //跳转教练员资格证书页面
    @RequestMapping(value = "instructor.html", method = RequestMethod.GET)
    public String instructor(Model model, Occupation occupation,PageResult p) {
        int num = occupationService.getOccupationCount(occupation.getIdNumber(), occupation.getIdNo());
        PageResult pr = new PageResult(p.getCurPage(), num);
        Map<String, Object> map = new HashMap<>();
        map.put("p", pr);
        map.put("qualifications", 2);
        map.put("idNumber", occupation.getIdNumber());
        map.put("idNo", occupation.getIdNo());
        List<Occupation> list = occupationService.getImageUrl(map);
        List<Occupation> list2 = occupationService.getImageUrl1(map);
        List list1 = new ArrayList();
        for (int i = 0; i < list2.size(); i++) {
            list1.add(i + 1);
        }
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        model.addAttribute("idNo", occupation.getIdNo());
        model.addAttribute("idNumber", occupation.getIdNumber());
        model.addAttribute("p", pr);
        return "qualification/instructor";
    }

    //跳转裁判员资格证书页面
    @RequestMapping(value = "referee.html", method = RequestMethod.GET)
    public String referee(Model model, Occupation occupation,PageResult p) {
        int num = occupationService.getOccupationCount(occupation.getIdNumber(), occupation.getIdNo());
        PageResult pr = new PageResult(p.getCurPage(), num);
        Map<String, Object> map = new HashMap<>();
        map.put("p", pr);
        map.put("qualifications", 3);
        map.put("idNumber", occupation.getIdNumber());
        map.put("idNo", occupation.getIdNo());
        List<Occupation> list = occupationService.getImageUrl(map);
        List<Occupation> list2 = occupationService.getImageUrl1(map);
        List list1 = new ArrayList();
        for (int i = 0; i < list2.size(); i++) {
            list1.add(i + 1);
        }
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        model.addAttribute("idNo", occupation.getIdNo());
        model.addAttribute("idNumber", occupation.getIdNumber());
        model.addAttribute("p", pr);
        return "qualification/referee";
    }

    //跳转解说员资格证书页面
    @RequestMapping(value = "commentator.html", method = RequestMethod.GET)
    public String commentator(Model model, Occupation occupation,PageResult p) {
        int num = occupationService.getOccupationCount(occupation.getIdNumber(), occupation.getIdNo());
        PageResult pr = new PageResult(p.getCurPage(), num);
        Map<String, Object> map = new HashMap<>();
        map.put("p", pr);
        map.put("qualifications", 4);
        map.put("idNumber", occupation.getIdNumber());
        map.put("idNo", occupation.getIdNo());
        List<Occupation> list = occupationService.getImageUrl(map);
        List<Occupation> list2 = occupationService.getImageUrl1(map);
        List list1 = new ArrayList();
        for (int i = 0; i < list2.size(); i++) {
            list1.add(i + 1);
        }
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        model.addAttribute("idNo", occupation.getIdNo());
        model.addAttribute("idNumber", occupation.getIdNumber());
        model.addAttribute("p", pr);
        return "qualification/commentator";
    }

    //跳转考评官资格证书页面
    @RequestMapping(value = "evaluation.html", method = RequestMethod.GET)
    public String evaluation(Model model, Occupation occupation,PageResult p) {
        int num = occupationService.getOccupationCount(occupation.getIdNumber(), occupation.getIdNo());
        PageResult pr = new PageResult(p.getCurPage(), num);
        Map<String, Object> map = new HashMap<>();
        map.put("p", pr);
        map.put("qualifications", 5);
        map.put("idNumber", occupation.getIdNumber());
        map.put("idNo", occupation.getIdNo());
        List<Occupation> list = occupationService.getImageUrl(map);
        List<Occupation> list2 = occupationService.getImageUrl1(map);
        List list1 = new ArrayList();
        for (int i = 0; i < list2.size(); i++) {
            list1.add(i + 1);
        }
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        model.addAttribute("idNo", occupation.getIdNo());
        model.addAttribute("idNumber", occupation.getIdNumber());
        model.addAttribute("p", pr);
        return "qualification/evaluation";
    }

    /**
     * @param file
     * @return 多文件上传 配合添加使用
     * @throws Exception
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Massage upload(@RequestParam("file") MultipartFile file) {
        return occupationImageService.upload(file);
    }

    /**
     * @param param1
     * @param param2
     * @return 信息添加
     */
    @RequestMapping(value = "occupationInsert", method = RequestMethod.POST)
    @ResponseBody
    public String occupationInsert(String param1, String param2, HttpSession session) {
        Occupation occupation = JSONObject.parseObject(param1, Occupation.class);
        if (getUser(session).getStatus() == 2) {
            occupation.setType(1);
            occupation.setFlag(4);
        } else {
            occupation.setType(2);
            occupation.setFlag(1);
        }
        return occupationService.occupationInsert(occupation, param2);
    }

    /**
     * @param page
     * @param limit
     * @param name
     * @param idNumber
     * @param regTime
     * @param city
     * @param qualifications
     * @param subject
     * @param subjectLevel
     * @param idNo
     * @return 查询 + 分页
     */
    @RequestMapping(value = "selectMyPage", method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Occupation> selectMyPage(

            @ApiParam(name = "page", value = "页码", required = true)
            @RequestParam int page,
            @ApiParam(name = "limit", value = "查询条数", required = true)
            @RequestParam int limit, String name, String idNumber, Long regTime, String city, Integer qualifications, Integer subject, Integer subjectLevel, String idNo, HttpSession session
    ) {
        return occupationService.selectMyPage(page, limit, idNo, idNumber, regTime, city, qualifications, subject, subjectLevel, name, session);
    }

    /**
     * @param occupation
     * @return 资格证书删除
     */
    @RequestMapping(value = "occupationDelete", method = RequestMethod.POST)
    @ResponseBody
    public Map occupationDelete(@RequestBody Occupation occupation, HttpSession session) {
        Occupation o = occupationService.getById(occupation.getId());
        File  file = new File(o.getImageUrl());
        if (file.isFile() && file.exists()) {
            file.delete();
        }
        if (getUser(session).getStatus() == 2) {
            if (occupationService.removeById(occupation)) {
                if (occupationImageService.removeByOccupationId(occupation.getId())) {
                    return ResponseUtil.getSuccessMap();
                }
            }
        } else {
            occupation.setType(2);
            occupation.setFlag(2);
            occupationService.updateById(occupation);
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    /**
     * @return 资格证书修改
     */
    @RequestMapping(value = "updateById", method = RequestMethod.POST)
    @ResponseBody
    public Map update(String param1, String param2, HttpSession session) {
        Occupation occupation = JSONObject.parseObject(param1, Occupation.class);
        if (getUser(session).getStatus() == 2) {
            occupation.setType(1);
            occupation.setFlag(4);
            return occupationService.updateOccupation(occupation, param2);
        } else {
            Occupation o = new Occupation();
            o.setName(occupation.getName());
            o.setIdNumber(occupation.getIdNumber());
            o.setSex(occupation.getSex());
            o.setRegTime(occupation.getRegTime());
            o.setCity(occupation.getCity());
            o.setQualifications(occupation.getQualifications());
            o.setBeforeId(occupation.getId());
            o.setIdNo(occupation.getIdNo());
            o.setImageUrl(occupation.getImageUrl());
            o.setFlag(3);
            o.setType(2);
            String[] split = param2.split("-");
            List list = new ArrayList();
            for (int i = 0; i < split.length; i++) {
                list.add(split[i]);
            }
            String param3 = "";
            for (int i = 0; i < list.size(); i++) {
                String arr = (String) list.get(i);
                String[] str = arr.split(",");
                param3 += str[1] + "," + str[2] + "-";
            }
            occupationService.occupationInsert(o, param3);
            return ResponseUtil.getSuccessMap();
        }
    }


    /**
     * @param occupation
     * @param session1
     * @return 资格证书登陆
     */
    @RequestMapping(value = "land", method = RequestMethod.POST)
    @ResponseBody
    public boolean login(Occupation occupation, OccupationImage occupationImage, HttpSession session1) {
        if (occupationService.login(occupation, occupationImage, session1)) {
            return true;
        }
        return false;
    }

    //批量删除
    @RequestMapping(value = "deleteBtId", method = RequestMethod.POST)
    @ResponseBody
    public Map deleteById(String ids, HttpSession session) {


        String[] a = ids.split(",");
        for (int i = 1; i < a.length; i++) {
            Occupation o = occupationService.getById(a[i]);
            File  file = new File(o.getImageUrl());
            if (file.isFile() && file.exists()) {
                file.delete();
            }
        }
        if (getUser(session).getStatus() == 2) {
            for (int i = 1; i < a.length; i++) {
                occupationService.removeById(a[i]);
                Occupation occupation = new Occupation();
                occupation.setId(Long.parseLong(a[i]));
                occupationImageService.removeByOccupationId(occupation.getId());
            }
            return ResponseUtil.getSuccessMap();
        } else {
            for (int i = 1; i < a.length; i++) {
                Occupation occupation = new Occupation();
                occupation.setId(Long.parseLong(a[i]));
                occupation.setType(2);
                occupation.setFlag(2);
                occupationService.updateById(occupation);
            }
            return ResponseUtil.getSuccessMap();

        }
    }

    /**
     * @param id
     * @return 回显所在职业资格的所属地区
     */
    @RequestMapping(value = "getOccupationCity", method = RequestMethod.POST)
    @ResponseBody
    public Map getOccupationCity(Long id) {
        return occupationService.getOccupationCity(id);
    }
//
//    /**
//     * @return 资格证书对应的 图片
//     */
//    @RequestMapping(value = "getOccupationUrl", method = RequestMethod.POST)
//    @ResponseBody
//    public Map getOccupationUrl(Occupation occupation) {
//        return occupationService.getImageUrl(occupation);
//    }

    //通过
    @RequestMapping(value = "adopt", method = RequestMethod.POST)
    @ResponseBody
    public Map abopt(@RequestBody Occupation occupation) {
        Occupation o = occupationService.getById(occupation);
        if (o.getFlag() == 2) {
            if (occupationService.removeById(occupation) && occupationImageService.removeByOccupationId(occupation.getId())) {
                return ResponseUtil.getSuccessMap();
            }
        } else if (o.getFlag() == 1) {
            occupation.setType(1);
            occupation.setFlag(4);
            if (occupationService.updateById(occupation)) {
                return ResponseUtil.getSuccessMap();
            }
        } else {
            occupation.setType(1);
            occupation.setFlag(4);
            occupationService.updateById(occupation);
            if (occupationService.removeById(o.getBeforeId()) && occupationImageService.removeByOccupationId(o.getBeforeId())) {
                return ResponseUtil.getSuccessMap();
            }
        }
        return ResponseUtil.getExceptionMap();
    }

    //不通过
    @RequestMapping(value = "noAdopt", method = RequestMethod.POST)
    @ResponseBody
    public Map noAdopt(@RequestBody Occupation occupation) {
        Occupation o = occupationService.getById(occupation);
        if (o.getFlag() == 2) {
            o.setFlag(4);
            o.setType(1);
            if (occupationService.updateById(o)) {
                return ResponseUtil.getSuccessMap();
            }

        } else if (o.getFlag() == 1) {
            if (occupationService.removeById(occupation) && occupationImageService.removeByOccupationId(occupation.getId())) {

                return ResponseUtil.getSuccessMap();
            }
        } else {
            if (occupationService.removeById(occupation) && occupationImageService.removeByOccupationId(occupation.getId())) {
                return ResponseUtil.getSuccessMap();
            }
        }

        return ResponseUtil.getExceptionMap();
    }

    //图片下载
    @RequestMapping(value = "Download")
    @ResponseBody
    public void Download(HttpServletResponse response, Long id) {
        Occupation occupation = occupationService.getById(id);
        String url = occupation.getImageUrl();
        String filename = occupation.getFileName();
        DownLoadFileUtils.downloadFile(response, url, filename);
    }
}
